package wicketcool.common.configuration;

import java.io.File;
import java.util.Map;

/**
 * User: rabbit
 * Date: Jul 30, 2009
 */
public final class WicketCoolConfiguration {

    private Map<String, String> configuration;

    public static final String DEFAULT_SRC_PATH = "src" + File.separator + "main" + File.separator + "java";
    public static final String CONFIGURATION_FILE_NAME = "configuration.properties";

    public WicketCoolConfiguration(Map<String, String> configuration) {
        this.configuration = configuration;
    }

    public void createDefaultsIfNeeded() {

        // domain

        if (getDomainModuleRoot() == null) {
            setDomainModuleRoot(getProjectName() + ".domain");
        }

        if (getEntitiesSourcePath() == null) {
            setEntitiesSourcePath(getDomainModuleRoot() + File.separator + DEFAULT_SRC_PATH
                    + File.separator + getPackageName() + File.separator + "domain" + File.separator + "entities");
        }

        if (getDaosSourcePath() == null) {
            setDaosSourcePath(getDomainModuleRoot() + File.separator + DEFAULT_SRC_PATH
                    + File.separator + getPackageName() + File.separator + "domain" + File.separator + "dao");
        }

        // service

        if (getServicesModuleRoot() == null) {
            setServicesModulePath(getProjectName() + ".service");
        }

        if (getServicesSourcePath() == null) {
            setServicesSourcePath(getServicesModuleRoot() + File.separator + DEFAULT_SRC_PATH
                    + File.separator + getPackageName() + File.separator + "service");
        }
    }

    public String getValue(String key) {
        return configuration.get(key);
    }


    /*
       Project name
    */
    public void setProjectName(String projectName) {
        configuration.put(ConfigurationKeys.PROJECT_NAME.key(), projectName);
    }

    public String getProjectName() {
        return configuration.get(ConfigurationKeys.PROJECT_NAME.key());
    }

    /*
        Package name
     */

    public void setPackageName(String packageName) {
        configuration.put(ConfigurationKeys.PACKAGE_NAME.key(), packageName);
    }

    public String getPackageName() {
        return configuration.get(ConfigurationKeys.PACKAGE_NAME.key());
    }

    /*
        Domain module configuration
     */

    public void setDomainModuleRoot(String domainModuleRoot) {
        configuration.put(ConfigurationKeys.DOMAIN_MODULE_ROOT.key(), domainModuleRoot);
    }

    public String getDomainModuleRoot() {
        return configuration.get(ConfigurationKeys.DOMAIN_MODULE_ROOT.key());
    }

    public void setEntitiesSourcePath(String entitiesPath) {
        configuration.put(ConfigurationKeys.ENTITIES_SOURCE_PATH.key(), entitiesPath);
    }

    public String getEntitiesSourcePath() {
        return configuration.get(ConfigurationKeys.ENTITIES_SOURCE_PATH.key());
    }

    public String getDaosSourcePath() {
        return configuration.get(ConfigurationKeys.DAOS_SOURCE_PATH.key());
    }

    public void setDaosSourcePath(String daoPath) {
        configuration.put(ConfigurationKeys.DAOS_SOURCE_PATH.key(), daoPath);
    }

    /*
        Service module configuration
     */

    public String getServicesModuleRoot() {
        return configuration.get(ConfigurationKeys.SERVICE_MODULE_ROOT.key());
    }

    public void setServicesModulePath(String serviceModuleRoot) {
        configuration.put(ConfigurationKeys.SERVICE_MODULE_ROOT.key(), serviceModuleRoot);
    }

    public String getServicesSourcePath() {
        return configuration.get(ConfigurationKeys.SERVICES_SOURCE_PATH.key());
    }

    public void setServicesSourcePath(String servicesPath) {
        configuration.put(ConfigurationKeys.SERVICES_SOURCE_PATH.key(), servicesPath);
    }
}
